<?php

class SystemSetting
{
    const Salt = "Salt";
    const Pepper = "Pepper";

    public function Salt(){
        return $this->GetValue(self::Salt);
    }

    public function Pepper(){
        return $this->GetValue(self::Pepper);
    }

    private function GetValue($key){
        $resources = Doctrine_Core::getTable('Setting')
            ->createQuery('s')
            ->select("s.key, s.value, type.name")
            ->innerJoin('s.settingtype type')
            ->where('s.visibility_id = "'.appcode\enum\Visibility::GetByName(appcode\enum\Visibility::V_public).'" && s.key = "'.$key.'"')
            ->fetchArray();
/*
        echo "<pre>";
        print_r($resources);
        echo "</pre>";
*/
        if(count($resources) == 0){
            return null;
        }
        else if(count($resources) > 1){
            throw new Exception("found more than one setting with key '".$key."'");
        }

        $setting = current($resources);

        return $setting["value"];
    }

    public function SetValue($key, $value, $type = 1){
        $setting = new Setting();
        $setting->key = $key;
        $setting->value = $value;
        $setting->settingtype_id = $type;
        $setting->visibility_id = appcode\enum\Visibility::GetDefaultId();
        $setting->save();
    }
}
